package com.leve.admin.common;

import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;

import java.util.List;

public class XiaomiPush {

    private static String APP_SECRET_KEY = Constants.APP_SECRET_KEY;
    private static String MY_PACKAGE_NAME = Constants.MY_PACKAGE_NAME;

    /**
     * @param title       信息栏标题
     * @param payload     消息内容
     * @param packageName app 包名
     * @param passThrough 设置消息是否通过透传的方式至App, 1表示透传消息, 0表示通知栏消息(默认是通知栏消息)
     * @param notifyType  1：使用默认提示音提示2：使用默认震动提示 4：使用默认led灯光提示
     * @return
     * @throws Exception
     */

    public static Message buildMessageByAndroid(String title, String payload, String packageName, int passThrough, int notifyType) {
        Message message = new Message.Builder()
                .title(title)
                .payload(payload)
                .restrictedPackageName(packageName)
                .passThrough(passThrough)
                .notifyType(notifyType)
                .build();
        return message;
    }


    /**
     * @param description 消息内容
     * @return
     * @throws Exception
     */
    public static Message buildMessageByIos(String description) {
        Message message = new Message.IOSBuilder()
                .description(description)
                .soundURL("default")    // 消息铃声
                .badge(1)               // 数字角标
                .category("action")     // 快速回复类别
                .build();
        return message;
    }

    /**
     * 群推送
     *
     * @param regids         小鬼id
     * @param description    详情
     * @param title          标题
     * @param messagePayload 内容
     * @throws Exception
     */
    public static Result sendMessageByRegIds(List<String> regids, String description, String title, String messagePayload) throws Exception {
        Sender sender = new Sender(APP_SECRET_KEY);
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(MY_PACKAGE_NAME)
                .notifyType(1)     // 使用默认提示音提示
                .build();
        return sender.send(message, regids, 3);//发送消息到一组设备上, regids个数不得超过1000个

    }

    public static Result sendMessageToAliases(List<String> alias, String messagePayload, String title, String description) throws Exception {
        Sender sender = new Sender(APP_SECRET_KEY);
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(MY_PACKAGE_NAME)
                .notifyType(1)     // 使用默认提示音提示
                .build();
        return sender.sendToAlias(message, alias, 3); //根据aliasList, 发送消息到指定设备上
    }


    public static Result sendMessageToAlias(String alias, String messagePayload, String title, String description) throws Exception {
        Sender sender = new Sender(APP_SECRET_KEY);
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(MY_PACKAGE_NAME)
                .notifyType(1)     // 使用默认提示音提示
                .build();
        return sender.sendToAlias(message, alias, 3); //根据alias, 发送消息到指定设备上
    }

}
